/* 
 * Copyright (C) 2011, WordCards4Me Project - http://code.google.com/p/wordcards4me/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.wordcards4me;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

public class MenuScreen extends List implements CommandListener {

	// Commands
	private Command openCommand = new Command("Open", Command.ITEM, 1);
	
	// Screens
	private FileBrowser fileBrowser;
	private SettingsScreen settingsScreen;
	
	private CardMidlet midlet;
	
	public MenuScreen(CardMidlet midlet) {
		super("Menu", List.IMPLICIT);
		
		this.midlet = midlet;
		
		append("Browse", CardMidlet.loadImage("/browser.png"));
		append("Settings", CardMidlet.loadImage("/settings.png"));
		append("Exit", CardMidlet.loadImage("/exit.png"));
		
		addCommand(openCommand);
		setCommandListener(this);
	}
	
	private FileBrowser getFileBrowser() {
		if(fileBrowser == null) {
			fileBrowser = new FileBrowser();
		}
		return fileBrowser;
	}
	
	public SettingsScreen getSettingsScreen() {
		if(settingsScreen == null) {
			settingsScreen = new SettingsScreen();
		}
		return settingsScreen;
	}

	public void commandAction(Command c, final Displayable d) {
		new Thread(new Runnable() {
			public void run() {
				try {
					switch (getSelectedIndex()) {
					case 0:
						CardMidlet.goForward(getFileBrowser());
						break;
					case 1:
						CardMidlet.goForward(getSettingsScreen());
						break;
					case 2:
						midlet.exit();
						break;
					default:
						break;
					}
				} catch (Exception e) {
					// TODO: show error screen
				}
			}
		}).start();
	}
}
